const express = require("express");

const router = express.Router();

const expressJoi = require("@escook/express-joi");

const path = require("path");
const multer = require("multer");
var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "uploads/");
  },
  filename: function (req, file, cb) {
    var singfileArray = file.originalname.split(".");
    var fileExtension = singfileArray[singfileArray.length - 1];
    cb(null, Date.now() + "." + fileExtension);
    console.log(file);
  },
});

var upload = multer({
  storage: storage,
  fileFilter: function fileFilter(req, file, cb) {
    // The function should call `cb` with a boolean
    // to indicate if the file should be accepted
    var singfileArray = file.originalname.split(".");
    var fileExtension = singfileArray[singfileArray.length - 1];
    if(fileExtension == 'png' || 'jpg') {
        cb(null, true);
    } else {
        cb(null, false);
        cb(new Error("文件类型不允许!"));
    }
    // To accept the file pass `true`, like so:
    // You can always pass an error if something goes wrong:
  },
});

const { addfile } = require("../control/avatar");
// const {} = require("../schema/user");

router.post("/avatar/:id", upload.single("avatar"), addfile);

module.exports = router;
